package binary_search;

/**
 * @Author: sfy
 * @Date: 2024/1/28 22:14
 * @Description: 二分查找
 */
public class Test {
    public static void main(String[] args) {
        int[] arr = {1,3,6,7,12,15,17};
        int target = 6;
        System.out.println(binarySearch(arr, target));
    }

    public static int binarySearch(int[] arr, int target){
        int i = 0;
        int j = arr.length-1;
        while(i<=j){
            int mid = (i+j>>>1);
            if(arr[mid]<target){
                i = mid+1;
            }else if(target<arr[mid]){
                j = mid -1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
